package com.tianji.aigc.tools;

import com.tianji.aigc.constant.ToolConstant;
import com.tianji.aigc.entity.PrePlaceOrder;
import com.tianji.api.client.trade.TradeClient;
import com.tianji.api.dto.trade.OrderConfirmVO;
import com.tianji.common.utils.UserContext;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 订单工具
 *
 * @Author mr.wu
 * @Date 2025-9-3 11:25
 */
@Component
public class OrderTool {

    @Autowired
    private TradeClient tradeClient;


    @Tool(description = ToolConstant.Tool.PRE_PLACE_ORDER_BY_COURSE_ID)
    public PrePlaceOrder prePlaceOrder(@ToolParam(description = ToolConstant.Tool_Param.COURSE_ID) Long courseId, ToolContext toolContext) {
        Long userId = (Long) toolContext.getContext().get(ToolConstant.USER_ID);//用户id
        UserContext.setUser(userId);//这里必须设置用户ID，否则会出现401错误
        OrderConfirmVO orderConfirmVO = tradeClient.prePlaceOrder(Arrays.asList(courseId));
        PrePlaceOrder prePlaceOrder = PrePlaceOrder.of(orderConfirmVO);

        String requestId = (String)toolContext.getContext().get(ToolConstant.REQUEST_ID);//请求id

        //key         field            value
        //requestId   prePlaceOrder    prePlaceOrder对象
        ToolResultHolder.put(requestId,"prePlaceOrder", prePlaceOrder);
        return prePlaceOrder;
    }
}
